﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.Generic;

namespace SGCG.Model
{
    public class FreeCurve : GroupBase, IHasBorder
    {
        public FreeCurve(GraphicObject parent) : base(parent)
        {
            LinkedList<GraphicObject> objList = new LinkedList<GraphicObject>();
            this.rawGraphicObjects = objList;
            GraphicObjects.AddLast(new Line(this)
                {
                    LineStyle = Model.LineStyle.Normal,
                    LineThickness = Model.LineThickness.Normal,
                    Color = Colors.Black
                });
        }

        protected BorderedGraphicObject FirstObject
        {
            get { return this.GraphicObjects.First() as BorderedGraphicObject; }
        }

        protected LinkedList<GraphicObject> GraphicObjects
        {
            get { return (LinkedList<GraphicObject>)this.rawGraphicObjects; }
            set
            {
                this.rawGraphicObjects = value;
                RaiseGraphicPropertyChanged("GraphicObjects");
            }
        }

        public int X0
        {
            get { return (GraphicObjects.First.Value as Line).X1; }
            set { (GraphicObjects.First.Value as Line).X1 = value; }
        }

        public int Y0
        {
            get { return (GraphicObjects.First.Value as Line).Y1; }
            set { (GraphicObjects.First.Value as Line).Y1 = value; }
        }

        public int X
        {
            get { return (GraphicObjects.Last.Value as Line).X2; }
            set { (GraphicObjects.Last.Value as Line).X2 = value; }
        }

        public int Y
        {
            get { return (GraphicObjects.Last.Value as Line).Y2; }
            set { (GraphicObjects.Last.Value as Line).Y2 = value; }
        }

        public void SetPoint(int x, int y)
        {
            int tmpX = X, tmpY = Y;
            (GraphicObjects.Last.Value as Line).X2 = x;
            (GraphicObjects.Last.Value as Line).Y2 = y;
            GraphicObjects.AddLast(new Line(this)
            {
                X1 = x,
                Y1 = y,
                X2 = tmpX,
                Y2 = tmpY,
                LineStyle = LineStyle,
                LineThickness = LineThickness,
                Color = Color
            }.Attach(DrawingCanvas));
        }

        public LineStyle LineStyle
        {
            get
            {
                return this.FirstObject.LineStyle;
            }
            set
            {
                this.GraphicObjects.OfType<IHasBorder>().Each(graphicObject => graphicObject.LineStyle = value);
            }
        }

        public LineThickness LineThickness
        {
            get
            {
                return this.FirstObject.LineThickness;
            }
            set
            {
                this.GraphicObjects.OfType<IHasBorder>().Each(graphicObject => graphicObject.LineThickness = value);
            }
        }

        public Color Color
        {
            get
            {
                return this.FirstObject.Color;
            }
            set
            {
                UserMessage.Debug("setting color to " + value);
                this.GraphicObjects.OfType<IHasBorder>().Each(graphicObject => graphicObject.Color = value);
            }
        }

        public Color OriginalColor { get { return FirstObject.OriginalColor; } }

        protected void CopyProperties(FreeCurve other)
        {
            base.CopyProperties(other);
            other.LineStyle = LineStyle;
            other.LineThickness = LineThickness;
            other.Color = OriginalColor;
            other.GraphicObjects = GraphicObjects;
        }

        public override GraphicObject CreateCopy()
        {
            FreeCurve freeCurve = new FreeCurve(null);
            CopyProperties(freeCurve);
            return freeCurve;
        }

        public override void Draw()
        {
            base.Draw();
        }
    }
}
